package com.group.sliding.controller;

import com.group.sliding.annotation.CacheAddByUserId;
import com.group.sliding.annotation.CacheRemoveByUserId;
import com.group.sliding.constant.Constants;
import com.group.sliding.domain.dto.CartFormDTO;
import com.group.sliding.domain.vo.CartVO;
import com.group.sliding.result.Result;
import com.group.sliding.service.ICartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@Api(tags = "购物车相关接口")
@RestController
@RequestMapping("/carts")
@RequiredArgsConstructor
public class CartController {
    private final ICartService cartService;

    @ApiOperation("添加商品到购物车")
    @PostMapping("/add")
    @CacheRemoveByUserId(keys = Constants.REDIS_KEY_CARTS)
    public Result<String> addItem2Cart(@Valid CartFormDTO cartFormDTO){
        log.info("添加商品到购物车 {}", cartFormDTO);
        cartService.addItem2Cart(cartFormDTO);
        log.info("成功添加商品到购物车 {}", cartFormDTO);
        return Result.success();
    }

    @ApiOperation("查询购物车列表")
    @GetMapping("/list")
    @CacheAddByUserId(key = Constants.REDIS_KEY_CARTS)
    public Result<List<CartVO>> queryMyCarts(){
        log.info("查询购物车列表");
        List<CartVO> cartVOS = cartService.queryMyCarts();
        log.info("查询购物车列表成功 {}", cartVOS);
        return Result.success(cartVOS);
    }
    
    @ApiOperation("批量删除购物车中商品")
    @ApiImplicitParam(name = "ids", value = "购物车内的商品的id集合")
    @DeleteMapping("/delete")
    @CacheRemoveByUserId(keys = Constants.REDIS_KEY_CARTS)
    public Result<String> deleteCartItemByIds(@RequestParam("ids") List<Long> ids){
        log.info("批量删除购物车中商品 ids: {}", ids);
        cartService.removeByItemIds(ids);
        log.info("批量删除购物车中商品成功 ids: {}", ids);
        return Result.success();
    }
}
